﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QualityRpt.Dao;
using System.Collections;
using System.Threading.Tasks;

namespace QualityRpt.Service
{
    /// <summary>
    /// 标签交易中间表
    /// </summary>
    public class OBUTransByDayService:BaseService
    {
        private static IOBUTransByDayDao _impDao = null;
        static OBUTransByDayService()
        {
            _impDao = CreateDaoInstance("IOBUTransByDayDao") as IOBUTransByDayDao;
        }
        /// <summary>
        /// 中间表的最终汇总集合
        /// </summary>
        public static DataTable GetOBUStatByMQ(DateTime startTime, DateTime endTime, string statIndic, int statTType,string spin,string specin,
            string cnin,string rdin,string plin,string lnin, bool statUnread)
        {
            string onepassCode = CodeConfigService.GetOnePassMode();
            string devExCode = CodeConfigService.GetOBUDevExModes();
            string lexCode = CodeConfigService.GetOBULExModes();
			string obuNocardCode = CodeConfigService.GetOBUNOCardMode ();
            if (statIndic.Trim() == string.Empty)//默认统计的指标
            {
                statIndic = "TRANSCOUNT,ONEPASS_TRANSCOUNT,ONEPASS_RATE,DEVSUC_TRANSCOUNT,DEVSUC_RATE,DEVEX_TRANSCOUNT,DEVEX_RATE,LEX_TRANSCOUNT,LEX_RATE";
                if (statUnread)
                {
                    statIndic = statIndic + ",UNREAD_TRANSCOUNT,UNREAD_RATE";
                }
            }
            #region 根据列启动任务
            List<Task> allTask = new List<Task>();
            //开启多个任务
            DataTable StatTable = null;
            Task<DataTable> statTask = new Task<DataTable>(() => _impDao.GetOBUStatByMQ(startTime,endTime,statTType,spin,specin,cnin,rdin,plin,lnin));
            statTask.Start(); allTask.Add(statTask);
            DataTable unreadTable = null; Task<DataTable> unreadTask = null;
            if (statUnread)//漏读交易指标
            {
                unreadTask = new Task<DataTable>(() => UNReadTransService.GetOBUStatByMQ(startTime,endTime,statTType,spin,specin,cnin,rdin,plin,lnin));
                unreadTask.Start(); allTask.Add(unreadTask);
            }
            //结束任务
            //等待任务线程完成
            Task.WaitAll(allTask.ToArray());
            StatTable = statTask.Result;
            if (unreadTask != null) { unreadTable = unreadTask.Result; }
            #endregion
            //组合结果集table
            DataView dv = new DataView(StatTable);
            List<string> columnNames = new List<string>();
            for (int i = 0; i < StatTable.Columns.Count - 2; i++)
            {
                columnNames.Add(StatTable.Columns[i].ColumnName);
            }
            DataTable resultTable = dv.ToTable(true, columnNames.ToArray());
            #region 加入指标列信息
            resultTable.Columns.Add("TRANSCOUNT", typeof(Int32));
            if (statUnread)//漏读交易指标
            {
                resultTable.Columns.Add("UNREAD_TRANSCOUNT", typeof(Int32));
                resultTable.Columns.Add("UNREAD_RATE", typeof(decimal), "iif(TRANSCOUNT>0,UNREAD_TRANSCOUNT/TRANSCOUNT,null)");
            }
            DataTable onePassTable = null;
            if (statIndic.Contains("ONEPASS_TRANSCOUNT") || statIndic.Contains("ONEPASS_RATE"))//一次通过率指标
            {
                onePassTable = new DataView(StatTable, "ICARD_WORKMODE IN (" + onepassCode + ")", string.Empty, DataViewRowState.CurrentRows).ToTable();
                resultTable.Columns.Add("ONEPASS_TRANSCOUNT", typeof(Int32));
                resultTable.Columns.Add("ONEPASS_RATE", typeof(decimal), "iif(TRANSCOUNT>0,ONEPASS_TRANSCOUNT/TRANSCOUNT,null)");
            }
            DataTable devExTable = null;
            if (statIndic.Contains("DEVEX_TRANSCOUNT") || statIndic.Contains("DEVEX_RATE"))//设备异常率指标
            {
                devExTable = new DataView(StatTable, "ICARD_WORKMODE IN (" + devExCode + ")", string.Empty, DataViewRowState.CurrentRows).ToTable();
                resultTable.Columns.Add("DEVEX_TRANSCOUNT", typeof(Int32));
                resultTable.Columns.Add("DEVEX_RATE", typeof(decimal), "iif(TRANSCOUNT>0,DEVEX_TRANSCOUNT/TRANSCOUNT,null)");
            }
            DataTable lExTable = null;
            if (statIndic.Contains("LEX_TRANSCOUNT") || statIndic.Contains("LEX_RATE"))//逻辑异常率指标
            {
                lExTable = new DataView(StatTable, "ICARD_WORKMODE IN (" + lexCode + ")", string.Empty, DataViewRowState.CurrentRows).ToTable();
                resultTable.Columns.Add("LEX_TRANSCOUNT", typeof(Int32));
                resultTable.Columns.Add("LEX_RATE", typeof(decimal), "iif(TRANSCOUNT>0,LEX_TRANSCOUNT/TRANSCOUNT,null)");
            }
			DataTable obuNocardTable = null;
			if(statIndic.Contains("DEVEX_RATE")|| statIndic.Contains("LEX_RATE")){
				obuNocardTable = new DataView(StatTable,"ICARD_WORKMODE IN (" + obuNocardCode + ")",string.Empty,DataViewRowState.CurrentRows).ToTable();
			}
			if (statIndic.Contains("DEVSUC_TRANSCOUNT") || statIndic.Contains("DEVSUC_RATE"))//设备成功率指标
			{
				resultTable.Columns.Add("DEVSUC_TRANSCOUNT", typeof(Int32),"ONEPASS_TRANSCOUNT+LEX_TRANSCOUNT");
				resultTable.Columns.Add("DEVSUC_RATE", typeof(decimal), "iif(TRANSCOUNT>0,DEVSUC_TRANSCOUNT/TRANSCOUNT,null)");
			}
            #endregion
            #region 计算赋值
            StringBuilder selText = new StringBuilder();
            for (int i = 0; i < resultTable.Rows.Count; i++)
            {
                selText.Clear(); DataRow curRow = resultTable.Rows[i];
                for (int j = 0; j < columnNames.Count; j++)
                {
                    string clName = columnNames[j];
                    DataColumn _dtc = resultTable.Columns[clName];
                    if (j > 0)
                    {
                        selText.Append(" AND ");
                    }
                    if (_dtc.DataType.Equals(typeof(string)))
                    {
                        selText.Append(clName + "='" + curRow[clName].ToString() + "'");
                    }
                    else
                    {
                        selText.Append(clName + "=" + curRow[clName].ToString());
                    }
                }
                object unread_sum_cnt = 0;
                int ii_temp = 0;
                object sum_cnt = StatTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                if (!int.TryParse(sum_cnt.ToString(), out ii_temp))
                {
                    sum_cnt = 0;
                }
                if (unreadTable != null)
                {
                    unread_sum_cnt = unreadTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                    if (!int.TryParse(unread_sum_cnt.ToString(), out ii_temp))
                    {
                        unread_sum_cnt = 0;
                    }
                }
                if (statUnread)
                {
                    if (unread_sum_cnt != DBNull.Value)
                    {
                        sum_cnt = (sum_cnt == DBNull.Value ? 0 : Convert.ToInt32(sum_cnt)) + Convert.ToInt32(unread_sum_cnt);
                    }
                }
                curRow["TRANSCOUNT"] = sum_cnt;
                if (resultTable.Columns.Contains("ONEPASS_TRANSCOUNT"))
                {
                    curRow["ONEPASS_TRANSCOUNT"] = onePassTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                }
				object obuNoCardCnt = obuNocardTable.Compute("sum(TRANSCOUNT)",selText.ToString());
				if(obuNoCardCnt == DBNull.Value){  obuNoCardCnt = 0; }
				if (resultTable.Columns.Contains("DEVEX_TRANSCOUNT"))//设备异常加obu无卡*0.5
				{
					object devex_cnt = devExTable.Compute("sum(TRANSCOUNT)", selText.ToString());
					if(devex_cnt == DBNull.Value){  devex_cnt = 0; }
					curRow["DEVEX_TRANSCOUNT"] = Convert.ToInt32(devex_cnt)
						+ Convert.ToInt32(Convert.ToDouble(obuNoCardCnt)*0.5);
				}
				if (resultTable.Columns.Contains("LEX_TRANSCOUNT"))//逻辑异常加obu无卡*0.5
				{
					object lex_cnt = lExTable.Compute("sum(TRANSCOUNT)", selText.ToString());
					if(lex_cnt == DBNull.Value){ lex_cnt = 0; }
					curRow["LEX_TRANSCOUNT"] = Convert.ToInt32(lex_cnt)
						+ Convert.ToInt32(Convert.ToDouble(obuNoCardCnt)*0.5);
				}
                if (resultTable.Columns.Contains("UNREAD_TRANSCOUNT"))
                {
                    curRow["UNREAD_TRANSCOUNT"] = unreadTable.Compute("sum(TRANSCOUNT)", selText.ToString());
                }
            }
            #endregion
            return resultTable;
        }
        /// <summary>
        /// 中交表的中间汇总集合包含icard_workmode分组的统计
        /// 可以按周按天按月统计
        /// </summary>
        public static DataTable GetOBUStatByMModeQ(DateTime startTime, DateTime endTime, int statTType, string spin, string specin,
            string cnin, string rdin, string plin, string lnin)
        {
            return _impDao.GetOBUStatByMQ(startTime, endTime, statTType, spin, specin, cnin, rdin, plin, lnin);
        }
    }
}
